import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/text/gt_text.dart';

enum GTVerticalStepsNodeStyle {
  /// 完成
  complete,

  /// 进行中
  process,

  /// 等待
  wait,

  /// 错误
  error,

  /// 成功
  success,

  /// 成功——黑底
  success_black,
}

extension _GTVerticalStepsNodeStyleExtension on GTVerticalStepsNodeStyle {
  Color get color {
    switch (this) {
      case GTVerticalStepsNodeStyle.success:
        return GTColorsV3.icon5.color;
      case GTVerticalStepsNodeStyle.error:
        return GTColorsV3.funct3.color;
      case GTVerticalStepsNodeStyle.wait:
        return GTColorsV3.icon3.color;
      case GTVerticalStepsNodeStyle.process:
        return GTColorsV3.icon5.color;
      case GTVerticalStepsNodeStyle.complete:
        return GTColorsV3.icon1.color;
      case GTVerticalStepsNodeStyle.success_black:
        return GTColorsV3.uikitText1V3.color;
      default:
        return GTColorsV3.uikitText1V3.color;
    }
  }
}

class GTVerticalStepsNode {
  /// 节点样式
  final GTVerticalStepsNodeStyle style;

  /// 标题
  final String text;

  /// 子内容视图,如果没有实现，则默认填充height:45的SizedBox
  final Widget? Function(BuildContext context)? nodeSubBuilder;

  const GTVerticalStepsNode({required this.text, this.nodeSubBuilder, required this.style});
}

class GTVerticalStepsV3 extends StatelessWidget {
  const GTVerticalStepsV3({super.key, required this.nodes});

  final List<GTVerticalStepsNode> nodes;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: nodes.map((e) => _buildItem(context, e)).toList(),
    );
  }

  Widget _buildItem(BuildContext context, GTVerticalStepsNode node) {
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              width: 20,
              height: 20,
              alignment: Alignment.center,
              decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: node.style.color),
              child: _buildSerialNumber(node),
            ),
            const SizedBox(width: 8),
            Expanded(
              child: Text(
                node.text,
                maxLines: 1,
                style: gtTextStyleW500(
                  color: node.style.color,
                  fontSize: 14,
                ),
              ),
            ),
          ],
        ),
        IntrinsicHeight(
          child: Row(
            children: [
              const SizedBox(width: 10),
              SizedBox(
                  height: double.infinity, child: node != nodes.last ? _buildDashLine() : const SizedBox(width: 1)),
              const SizedBox(width: 17),
              Expanded(child: node.nodeSubBuilder?.call(context) ?? const SizedBox(height: 45)),
            ],
          ),
        ),
      ],
    );
  }

  /// 构建序号
  Widget _buildSerialNumber(GTVerticalStepsNode node) {
    int index = nodes.indexOf(node);
    if (node.style == GTVerticalStepsNodeStyle.error) {
      return Icon(GTIconFont.e8e0, color: GTColorsV3.uikitIcon6V3.color, size: 12);
    }
    if (node.style == GTVerticalStepsNodeStyle.success) {
      return Icon(GTIconFont.e8df, color: GTColorsV3.uikitIcon6V3.color, size: 12);
    }
    if (node.style == GTVerticalStepsNodeStyle.success_black) {
      return Icon(GTIconFont.e8df, color: GTColorsV3.icon8.color, size: 12);
    }
    return GTText(
      (index + 1).toString(),
      removeTopBottomPadding: true,
      maxLines: 1,
      style: gtTextStyleW500(
        color: node.style == GTVerticalStepsNodeStyle.complete || node.style == GTVerticalStepsNodeStyle.success_black
            ? GTColorsV3.icon8.color
            : GTColorsV3.text6.color,
        fontSize: 16,
      ).copyWith(height: 1),
    );
  }

  /// 虚线
  Widget _buildDashLine() {
    return SizedBox(
      width: 1,
      child: CustomPaint(painter: _DashedLinePainter()),
    );
  }
}

class _DashedLinePainter extends CustomPainter {
  static const double dashHeight = 3;
  static const double dashSpace = 2;
  static const double lineWidth = 1;
  final Color lineColor = GTColorsV3.uikitDivider4V3.color;

  _DashedLinePainter();

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = lineColor
      ..strokeWidth = lineWidth
      ..style = PaintingStyle.stroke;

    double startY = 0.0;
    while (startY < size.height) {
      canvas.drawLine(
        Offset(lineWidth / 2, startY),
        Offset(lineWidth / 2, startY + dashHeight),
        paint,
      );
      startY += dashHeight + dashSpace;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
